package com.fengniao.goods.controller;

import com.fengniao.goods.pojo.Album;
import com.fengniao.goods.service.AlbumService;
import com.github.pagehelper.PageInfo;
import entity.DBOperUtil;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lanhai
 * @date 2021/7/5 16:41
 */
@RestController
@RequestMapping("/album")
@CrossOrigin
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    @GetMapping
    public Result<List<Album>> findAll() {
        List<Album> all = albumService.findAll();
        return Result.data(all);
    }

    @GetMapping("/{id}")
    public Result<Album> findById(@PathVariable Long id) {

        return Result.data(albumService.findById(id));
    }

    @PostMapping
    public Result add(@RequestBody Album Album) {

        int flag = albumService.add(Album);
        DBOperUtil.checkDBOper(flag, "添加相册失败");
        return Result.ok("添加相册成功");
    }

    @PutMapping("/{id}")
    public Result revise(@PathVariable Long id, @RequestBody Album Album) {

        Album.setId(id);
        int flag = albumService.revise(Album);
        DBOperUtil.checkDBOper(flag, "添加相册失败");
        return Result.ok("修改相册成功");
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id) {

        int flag = albumService.remove(id);
        DBOperUtil.checkDBOper(flag,"删除相册失败");
        return Result.ok("删除相册成功");
    }

    @PostMapping("/search")
    public Result<List<Album>> findList(@RequestBody Album Album) {

        List<Album> list = albumService.findList(Album);
        return Result.data(list);
    }

    @GetMapping("/search/{num}/{size}")
    public Result<PageInfo<Album>> findPage(@PathVariable("num") Integer num,
                                            @PathVariable("size") Integer size) {

        PageInfo<Album> page = albumService.findPage(num, size);
        return Result.data(page);
    }

    @PostMapping("/search/{num}/{size}")
    public Result<PageInfo<Album>> findPage(@RequestBody(required = false) Album album,
                                            @PathVariable("num") Integer num,
                                            @PathVariable("size") Integer size) {

        PageInfo<Album> page = albumService.findPage(album, num, size);
        return Result.data(page);
    }
}
